/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content,
    duration: 5000
  });
}
/**
 * 获取当前年月日
 * @param {Object} date
 */
export function getCurrenDate() {
  let curDate = new Date();
  let curYear = curDate.getFullYear();
  let curMonth = curDate.getMonth() + 1;
  let curMonthdays = new Date(curYear, curMonth, 0).getDate();
  let curDay = curDate.getDate();
  let curHour = curDate.getHours();
  let curMinute = curDate.getMinutes();
  let curSecond = curDate.getSeconds();
  return {
    curDate,
    curYear,
    curMonth,
    curMonthdays,
    curDay,
    curHour,
    curMinute,
    curSecond
  };
}
/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        resolve(res);
      }
    });
  });
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = '';
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + '=';
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== '' &&
            typeof value[key] !== 'undefined'
          ) {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + '=';
            result += subPart + encodeURIComponent(value[key]) + '&';
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&';
      }
    }
  }
  return result;
}
/**
 * 字典参数处理
 * @param params 参数
 */
export function dictTansParams(datas, name) {
  let results = [];
  let data = datas || [];
  data.forEach((item) => {
    if (item.dictType == name) {
      results.push({
        value: item.value,
        text: item.label
      });
    }
  });
  return results;
}
export function getDate(date, addZero = true) {
  date = new Date(date);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return `${year}-${addZero ? addZeroFun(month) : month}-${
    addZero ? addZeroFun(day) : day
  }`;
}
export function addZeroFun(num) {
  if (num < 10) {
    num = `0${num}`;
  }
  return num;
}
export function timestampToChineseDate(timestamp) {
  const date = new Date(timestamp);

  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份是从0开始的
  const day = date.getDate();

  return `${year}-${addZeroFun(month)}-${addZeroFun(day)}`;
}

export function timestampToChineseTime(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month =
    (date.getMonth() + 1).toString().length > 1
      ? date.getMonth() + 1
      : `0${date.getMonth() + 1}`;
  const day =
    date.getDate().toString().length > 1
      ? date.getDate()
      : `0${date.getDate()}`;
  const hour =
    date.getHours().toString().length > 1
      ? date.getHours()
      : `0${date.getHours()}`;
  const minute =
    date.getMinutes().toString().length > 1
      ? date.getMinutes()
      : `0${date.getMinutes()}`;
  const second =
    date.getSeconds().toString().length > 1
      ? date.getSeconds()
      : `0${date.getSeconds()}`;
  return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
}

export function timestampToFormatter(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  const hour = date.getHours().toString().padStart(2, '0');
  const minute = date.getMinutes().toString().padStart(2, '0');
  const second = date.getSeconds().toString().padStart(2, '0');
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

export async function authIsPass(authValue) {
  try {
    const res = await new Promise((resolve, reject) => {
      uni.getSetting({
        success: (res) => {
          resolve(res);
        },
        fail: (err) => {
          reject(err);
        }
      });
    });
    const { authSetting } = res;
    if (typeof authValue === 'string') {
      if (authSetting[authValue]) {
        return true;
      } else {
        return false;
      }
    }
    if (Array.isArray(authValue)) {
      let noPassList = authValue.filter((key) => !authSetting[key]);
      if (noPassList.length > 0) {
        return noPassList;
      } else {
        return [];
      }
    }
  } catch (err) {
    return false;
  }
}
